import KoaRouter from 'koa-router'
import Config from '@/config/Config'
import verifyToken from '@/middlewares/verifyToken'
import commonDel from '@/api/v1/common/single-table/del'
import { command } from '@/server/mysql'
const router = new KoaRouter({
  prefix: `${Config.API_PREFIX}v1/system/role`,
})
router.post(
  '/del',
  verifyToken,
  commonDel({
    tableName: 'system_role',
    async beforeFn(where: string) {
      // 删除关联数据
      const resourceSql = `DELETE FROM system_role_resource ${where
        .replace('id IN ', 'role_id IN ')
        .replace('id = ', 'role_id = ')};`
      const userSql = `DELETE FROM system_user_role ${where
        .replace('id IN ', 'role_id IN ')
        .replace('id = ', 'role_id = ')};`
      ;(await command(`${resourceSql}${userSql}`, true)).results
    },
  }),
)
export default router
